home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 014 / swapname.arc / SWAPNAME.ASM next >
Encoding:
Assembly Source File  |  1986-02-20  |  5.4 KB  |  261 lines

  1.     page    80,132
  2.     title    SWAP - switch names of two files
  3.     comment |
  4.  
  5.  
  6. Name:    'swap' two file names.
  7.  
  8. Usage:
  9.     swap  [d:][path]filename1  [d:][path]filename2
  10.  
  11. Remarks:
  12.     The two files may be on different drives, and/or in
  13.     different sub-directories.
  14.  
  15.     Both file names must be supplied. If drive and/or path
  16.     names are omitted, the current drive/path is used.
  17.  
  18.     Written by V.Buerg for the IBM PC using DOS 2.0 or later.
  19.     Public domain. Feb 20, 1986.                |
  20.  
  21.  
  22. rename    macro    old,new
  23.     mov    dx,offset old        ; original file name
  24.     mov    di,offset new        ;  to new file name
  25.     mov    ah,56h            ; dos rename function
  26.     int    21h
  27.     endm
  28.  
  29.  
  30. cseg    segment    public byte 'code'
  31.     assume    cs:cseg, ds:cseg, es:nothing
  32.     org    100h
  33.  
  34. swap    proc    far
  35.     jmp    start            ; skip around data for masm
  36.  
  37. cr    equ    31
  38. lf    equ    10
  39. stopper    equ    255
  40.  
  41. errlvl    db    0            ; dos return code
  42.  
  43. usage    db    cr,lf
  44.     db    'Usage:  swap  [d:][path]filename  [d:][path]filename'
  45.     db    cr,lf,stopper
  46.  
  47. errors    db    cr,lf,'RENAME failed, return code = '
  48. rc    dw    0
  49.     db    cr,lf,stopper
  50.  
  51. ptr1    dw    0            ; offset to filename part
  52. ptr2    dw    0
  53.  
  54. tempnam    db    '@-@-@-@-.$$$',0        ; temporary name
  55.  
  56. file1    db    76 dup (0)        ; first file name
  57.                     ; d1:path1\file1
  58.  
  59. file2    db    76 dup (0)        ; second file name
  60.                     ; d2:path2\file2
  61.  
  62. temp    db    76 dup (0)        ; intermediate file 1 name
  63.                     ; d1:path1\@-@-@-@-.$$$
  64.  
  65. new1    db    76 dup (0)        ; new name for first file
  66.                     ; d1:path1\file2
  67.  
  68. new2    db    76 dup (0)        ; new name for second file
  69.                     ; d2:path2\file1
  70.     page
  71.  
  72. start:
  73.     mov    ah,19h            ; get current drive number
  74.     int    21h
  75.     add    al,'A'            ; make into drive letter
  76.     mov    ah,':'
  77.     mov    word ptr new1,ax    ;  and set into filenames
  78.     mov    word ptr new2,ax
  79.     mov    word ptr file1,ax
  80.     mov    word ptr file2,ax
  81.  
  82.     mov    si,81h            ; point to command line
  83.     sub    cx,cx
  84.     or    cl,byte ptr -1[si]    ; get and test length
  85.     jnz    parms
  86. parm_error:
  87.     mov    dx,offset usage        ; display usage info
  88.     call    prints            ;  maybe they'll get it right
  89.     jmp    exit            ;  next time
  90.  
  91. ;    get the command line file names
  92.  
  93. parms:
  94.     mov    di,offset file1        ; build first d:path\filename
  95.     call    getparm
  96.  
  97.     mov    di,offset file2        ; build second name
  98.     call    getparm
  99.  
  100.     mov    si,offset file1        ; set up temp file 1 name
  101.     mov    di,offset temp
  102.     mov    cx,76
  103.     rep    movsb
  104.  
  105. ;    set up temporary name with drive/path from original filename
  106.  
  107.     mov    si,offset file1        ; first file name
  108.     mov    di,offset new1        ; change name to that of file 2
  109.     call    getpath
  110.     mov    ptr1,si            ; save offset to file name
  111.  
  112.     mov    si,offset file2        ; second file name
  113.     mov    di,offset new2        ; change name to that of file 1
  114.     call    getpath
  115.     mov    ptr2,si            ; save offset to file name
  116.  
  117. ;    swap the file name parts
  118.  
  119.     mov    bx,ptr1            ; offset to file 1 name
  120.     mov    bp,ptr2            ; offset to file 2 name
  121.  
  122.     lea    di,word ptr temp[bx]    ; set temp name from file 1
  123.     mov    si,offset tempnam
  124.     mov    cx,13            ; just lazy
  125.     rep    movsb
  126.  
  127.     lea    si,word ptr file1[bx]     ; set new file 2 name from file 1 name
  128.     lea    di,word ptr cs:new2[bp]
  129.     mov    cx,13
  130.     rep    movsb
  131.  
  132.     lea    si,word ptr cs:file2[bp] ; set new file 1 name from file 1 name
  133.     lea    di,word ptr new1[bx]
  134.     mov    cx,13
  135.     rep    movsb
  136.  
  137. ;    finally, do the renaming
  138.  
  139.     rename    file1,temp
  140.     jnc    do2            ; check for error return
  141.     mov    errlvl,al
  142.     jmp    abort            ; oops, return al as error level
  143.  
  144. do2:
  145.     rename    file2,new2        ; file2 becomes file1 name
  146.     jnc    do3            ; check for error return
  147.     mov    errlvl,al
  148.     rename    temp,file1        ; undo first rename
  149.     jmp    abort            ; return al as error level
  150.  
  151. do3:
  152.     rename    temp,new1        ; file1 becomes file2 name
  153.     jnc    done            ; check for error return
  154.     mov    errlvl,al        ; save return code
  155.     rename    new2,file2        ; undo previous renames
  156.     rename    temp,file1
  157.     jmp    abort            ; return al as error level
  158.  
  159. done:
  160.     sub    al,al            ; zero return code
  161.     jmp    exit
  162.  
  163. ;    normal and abnormal exits
  164.  
  165. abort:
  166.     mov    al,errlvl        ; get rename return code
  167.     aam
  168.     xchg    ah,al
  169.     or    ax,'00'
  170.     mov    rc,ax            ; clean up the return code
  171.     mov    dx,offset errors    ; and display a nasty message
  172.     call    prints
  173. exit:
  174.     mov    al,errlvl
  175.     mov    ah,4ch            ; exit to dos
  176.     int    21h
  177.  
  178.     subttl    - subroutines
  179.     page
  180. ;
  181. ;    get next command line operand
  182. ;
  183. ;    si points to current char in cmd line
  184. ;    di points to target filename field
  185.  
  186. getparm    proc    near            ; get next filename operand
  187.  
  188. parm1:
  189.     lodsb                ; strip leading delimiters
  190.     cmp    al,' '
  191.     je    parm2             ; blanks
  192.     cmp    al,9
  193.     jne    parm3            ; tabs
  194. parm2:    loop    parm1
  195.     jcxz    parm_bad        ; nobody home
  196.  
  197. parm3:
  198.     cmp    byte ptr [si],':'    ; does it have a drive?
  199.     je    parm4
  200.     add    di,2
  201. parm4:    stosb
  202.     lodsb                ; copy first file name
  203.     cmp    al,' '            ; check for delimiter
  204.     jbe    parm6
  205.     loop    parm4
  206. parm_bad:
  207.     jmp    parm_error        ; oops, ran out of data
  208. parm6:
  209.     ret
  210. getparm    endp
  211.  
  212. ;    get drive and path from original file name
  213. ;    and use as prefix for file names used by rename
  214.  
  215. getpath    proc    near
  216.     push    si
  217.     mov    cx,76            ; copy original to new name
  218.     rep    movsb
  219.  
  220.     std
  221.     mov    cx,76            ; look for ending delimiter
  222. get_file3:
  223.     lodsb
  224.     cmp    al,'\'            ; bumped into path?
  225.     je    got_file3
  226.     cmp    al,':'            ; or bumped into drive?
  227.     je    got_file3
  228.     loop    get_file3
  229. got_file3:
  230.     inc    si            ; ptr to filename part
  231.     inc    si
  232.     pop    ax
  233.     sub    si,ax            ; return offset
  234.     cld
  235.     ret
  236. getpath    endp
  237.  
  238. ;
  239. ;    print string like int 21h/9
  240.  
  241. prints    proc    near
  242.     sub    cx,cx            ; length of text
  243.     mov    si,dx            ; pointer to text
  244. prints1:
  245.     lodsb
  246.     cmp    al,stopper        ; end of string?
  247.     je    print9
  248.     inc    cx
  249.     jmp    prints1
  250. print9:
  251.     mov    bx,1            ; use stdout handle
  252.     mov    ah,40h            ; write to file handle
  253.     int    21h
  254.     ret
  255. prints    endp
  256.  
  257. swap    endp
  258.  
  259. cseg    ends
  260.     end    swap
  261.